
const db = require("../db/index");
const fs = require("fs");
const bcrypt = require("bcryptjs")


exports.uploadAvatar = (req, res) => {
    const oldName = req.files[0].filename;
    const newName = req.files[0].originalname;

    fs.renameSync(`./public/upload/${oldName}`, `./public/upload/${oldName}-${newName}`)

    res.send({
        status: 200,
        data: {
            imageUrl: `http://127.0.0.1:3007/upload${oldName}-${newName}`,
            fileName: oldName,
            originalName: newName,
            size: req.files[0].size,
            mimeType: req.files[0].mimetype,
            userId: req.auth.id,
        }
    })

}


exports.saveSystemConfig = (req, res) => {
    const { imageInfo, account, password, name, sex, identity, deptName, email } = req.body;
    const id = req.auth.id;// 用户id


    const checkStr = "select * from users where account = ?";
    db.query(checkStr, account, (error, results) => {
        if (error) {
            return res.errFunc(error)
        }
        if (results.length > 0 && results[0].id != id) {
            return res.errFunc("账户名重复")
        }
        const updateStr = "update users set ? where id = ?";
        const data = {
            imageUrl: imageInfo.imageUrl,
            account,
            password: bcrypt.hashSync(password, 10),
            sex,
            name,
            identity,
            deptName,
            email,
        }
        db.query(updateStr, [data, id], (updateError, updateResults) => {
            if (updateError) {
                return res.errFunc(updateError);
            }
            if (updateResults.affectedRows != 1) {
                return res.errFunc("跟新失败，请从新提交")
            }

            const checkImageStr = "select * from image where userId = ?";
            const updateImageStr = "update image set ? where userId = ?";
            const insertImageStr = "insert into image set ?"

            db.query(checkImageStr, id, (checkImageErr, checkImageResult) => {

                if (checkImageErr) {
                    return res.errFunc(checkImageErr);
                }

                if (checkImageResult.length == 0) {
                    db.query(insertImageStr, imageInfo, (insertImageErr, insertImageResult) => {
                        if (insertImageErr) {
                            return res.errFunc(insertImageErr);
                        }
                        if (insertImageResult.affectedRows != 1) {
                            return res.errFunc("跟新失败，请从新提交")
                        }
                        res.send({ status: 200, message: "编辑成功" })
                    })
                } else {
                    db.query(updateImageStr, [imageInfo, id], (updateImageErr, updateImageResult) => {
                        if (updateImageErr) {
                            return res.errFunc(insertImageErr);
                        }
                        if (updateImageResult.affectedRows != 1) {
                            return res.errFunc("跟新失败，请从新提交")
                        }
                        res.send({ status: 200, message: "编辑成功" })
                    })
                }
            })


        })
    })



}


